<script setup>
import { ref, onMounted, onUnmounted } from 'vue'
import { useRouter } from 'vue-router'
import axios from 'axios'
import { ElMessage } from 'element-plus'

const router = useRouter()

const loginForm = ref({
  username: '',
  password: ''
})

const loginBoxRef = ref(null)
const tiltEffect = ref({ x: 0, y: 0 })

const handleMouseMove = (e) => {
  if (!loginBoxRef.value) return
  
  const rect = loginBoxRef.value.getBoundingClientRect()
  const centerX = rect.left + rect.width / 2
  const centerY = rect.top + rect.height / 2
  
  tiltEffect.value = {
    x: -(e.clientX - centerX) / 20,
    y: -(e.clientY - centerY) / 20
  }
}

onMounted(() => {
  window.addEventListener('mousemove', handleMouseMove)
})

onUnmounted(() => {
  window.removeEventListener('mousemove', handleMouseMove)
})

const formRef = ref(null)

const rules = {
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
}

const handleLogin = async () => {
  if (!formRef.value) return
  
  await formRef.value.validate(async (valid) => {
    if (valid) {
      try {
        const response = await axios.post(`${import.meta.env.VITE_API_BASE_URL}/api/login`, {
          username: loginForm.value.username,
          password: loginForm.value.password
        })
        
        if (response.data.code === 200) {
          ElMessage.success('登录成功')
          localStorage.setItem('token', response.data.data.token)
          router.push('/books')
        }
      } catch (error) {
        console.error('登录失败:', error)
        ElMessage.error('登录失败')
      }
    }
  })
}
</script>

<template>
  <div class="login-container">
    <div class="login-box" ref="loginBoxRef" :style="{
  transform: `perspective(1000px) rotateX(${tiltEffect.y}deg) rotateY(${-tiltEffect.x}deg)`
}">
      <h2>登录</h2>
      <el-form
        ref="formRef"
        :model="loginForm"
        :rules="rules"
        label-width="80px"
        class="login-form"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="loginForm.username" placeholder="请输入用户名" />
        </el-form-item>
        
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            show-password
          />
        </el-form-item>
        
        <el-form-item>
          <el-button type="primary" @click="handleLogin" class="login-button">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: url('/login-bg.png') no-repeat center center;
  background-size: cover;
}

.login-box {
  width: 400px;
  padding: 30px;
  background-color: rgba(255, 255, 255, 0.2);
  backdrop-filter: blur(10px);
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  transition: transform 0.1s ease;
  transform-style: preserve-3d;
}

.login-box h2 {
  text-align: center;
  margin-bottom: 30px;
  color: #333;
  text-shadow: 0 0 5px rgba(64, 158, 255, 0.7);
  transition: text-shadow 0.3s ease;
}

.login-box h2:hover {
  text-shadow: 0 0 10px rgba(64, 158, 255, 0.9);
}

.el-form-item__label {
  text-shadow: 0 0 3px rgba(64, 158, 255, 0.5);
  transition: text-shadow 0.3s ease;
}

.el-form-item__label:hover {
  text-shadow: 0 0 6px rgba(64, 158, 255, 0.7);
}

.login-form {
  margin-top: 20px;
}

.login-button {
  width: 100%;
  transition: all 0.3s ease;
}

.login-button:hover {
  box-shadow: 0 0 10px 2px rgba(64, 158, 255, 0.7);
}

.el-input__wrapper {
  transition: all 0.3s ease;
}

.el-input__wrapper:hover,
.el-input__wrapper.is-focus {
  box-shadow: 0 0 10px 2px rgba(64, 158, 255, 0.5);
}
</style>